home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpat2-1.000 / xpat2-1 / xpat2-1.04 / src / X-widget.c < prev    next >
C/C++ Source or Header  |  1995-10-12  |  11KB  |  296 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    X patience version 2 -- module Xaw-widget.c                 */
  5. /*                                         */
  6. /*    A pseudo-widget for the Athena Widget interface                 */
  7. /*    written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    March-1994                                 */
  9. /*    see COPYRIGHT.xpat2 for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #if defined(useXaw) || defined(useMotif)
  14. #include "X-pat.h"
  15.  
  16. #include <X11/IntrinsicP.h>
  17. #include <X11/StringDefs.h>
  18. #include "TableauP.h"
  19.  
  20. static XtResource resources[] = {
  21. #define offset(field) XtOffsetOf(TableauRec, tableau.field)
  22.     /* {name, class, type, size, offset, default_type, default_addr}, */
  23.     {XtNmessageFile,XtCMessageFile,XtRString,sizeof(String),  offset(messagefile), XtRString, NULL},
  24.     {XtNkeyboardFile,XtCKeyboardFile,XtRString,sizeof(String),offset(keyboardfile),XtRString, NULL},
  25.     {XtNxpmdir,  XtCXpmdir,  XtRString,    sizeof(String),    offset(xpmdir),   XtRString,    NULL},
  26. #ifdef LOADCARDS
  27.     {XtNcardset, XtCCardset, XtRString,    sizeof(String),    offset(cardset),  XtRString,    NULL},
  28. #endif
  29.     {XtNruleset, XtCRuleset, XtRString,    sizeof(String),    offset(rules),    XtRString,    NULL},
  30.     {XtNfaceup,     XtCFaceup,  XtRInt,       sizeof(int),       offset(faceup),    XtRImmediate, (XtPointer)-1},
  31.     {XtNfacedown,XtCFacedown,XtRInt,       sizeof(int),       offset(facedown),    XtRImmediate, (XtPointer)-1},
  32.     {XtNdecks,   XtCDecks,   XtRInt,       sizeof(int),       offset(decks),    XtRImmediate, (XtPointer)-1},
  33.     {XtNslots,   XtCSlots,   XtRInt,       sizeof(int),       offset(slots),    XtRImmediate, (XtPointer)-1},
  34.     {XtNjokers,  XtCJokers,  XtRInt,       sizeof(int),       offset(jokers),    XtRImmediate, (XtPointer)-1},
  35.     {XtNtmps,     XtCTmps,    XtRInt,       sizeof(int),       offset(tmps),    XtRImmediate, (XtPointer)-1},
  36.     {XtNparam0,     XtCParam,   XtRInt,       sizeof(int),       offset(param0),    XtRImmediate, (XtPointer)-1},
  37.     {XtNparam1,     XtCParam,   XtRInt,       sizeof(int),       offset(param1),    XtRImmediate, (XtPointer)-1},
  38.     {XtNparam2,     XtCParam,   XtRInt,       sizeof(int),       offset(param2),    XtRImmediate, (XtPointer)-1},
  39.     {XtNparam3,     XtCParam,   XtRInt,       sizeof(int),       offset(param3),    XtRImmediate, (XtPointer)-1},
  40.     {XtNgapx,     XtCGap,     XtRDimension, sizeof(Dimension), offset(xgap),    XtRImmediate, (XtPointer)4},
  41.     {XtNgapy,     XtCGap,     XtRDimension, sizeof(Dimension), offset(ygap),    XtRImmediate, (XtPointer)4},
  42.     {XtNarrw,     XtCArr,     XtRDimension, sizeof(Dimension), offset(arrowwidth),XtRImmediate, (XtPointer)255},
  43.     {XtNarrh,     XtCArr,     XtRDimension, sizeof(Dimension), offset(arrowheight),XtRImmediate, (XtPointer)255},
  44.     {XtNmaWidth, XtCMaWidth, XtRDimension, sizeof(Dimension), offset(markwidth), XtRImmediate, (XtPointer)999},
  45.     {XtNcbColor, XtCCbColor, XtRPixel,  sizeof(Pixel),  offset(cbcolor),    XtRString, "DarkGreen"},
  46.     {XtNmaColor, XtCMaColor, XtRPixel,  sizeof(Pixel),  offset(markcolor),  XtRString, XtDefaultBackground},
  47.     {XtNarColor, XtCArColor, XtRPixel,  sizeof(Pixel),  offset(arrowcolor), XtRString, "White"},
  48.     {XtNredColor,XtCRedColor,XtRPixel,  sizeof(Pixel),  offset(redcolor),   XtRString, "Red3"},
  49.     {XtNrng,     XtCRng,   XtRInt,       sizeof(int),       offset(rng),    XtRImmediate, (XtPointer)1},
  50. #undef offset
  51. };
  52.  
  53.  
  54. /* actions on the desktop area */
  55. /*ARGSUSED*/
  56. static void TableauKey(Widget w, XEvent *xev, String *s, Cardinal *num) {
  57.     key_press((XKeyPressedEvent *)xev);
  58. }
  59. /*ARGSUSED*/
  60. static void TableauBtn_up(Widget w, XEvent *xev, String *s, Cardinal *num) {
  61.     button_release((XButtonPressedEvent *)xev);
  62. }
  63. /*ARGSUSED*/
  64. static void TableauBtn_down(Widget w, XEvent *xev, String *s, Cardinal *num) {
  65.     button_press((XButtonPressedEvent *)xev);
  66. }
  67. /*ARGSUSED*/
  68. static void TableauMove(Widget w, XEvent *xev, String *s, Cardinal *num) {
  69.     mouse_motion((XPointerMovedEvent *)xev);
  70. }
  71. /*ARGSUSED*/
  72. static void Redisplay(Widget w, XEvent *xev, Region region) {
  73.     redraw_table((XExposeEvent *)xev);
  74. }
  75.  
  76. static XtActionsRec actions[] = {
  77.     /* {name, procedure}, */
  78.     { "tableau_k",    TableauKey },
  79.     { "tableau_d",    TableauBtn_down },
  80.     { "tableau_u",    TableauBtn_up },
  81.     { "tableau_m",    TableauMove },
  82. };
  83.  
  84. static char translations[] = "\
  85. <Key>:        tableau_k()    \n\
  86. <BtnDown>:    tableau_d()    \n\
  87. <BtnUp>:    tableau_u()    \n\
  88. <Btn3Motion>:    tableau_m()    \n\
  89. ";
  90.  
  91.  
  92. static Boolean SetValues(Widget current, Widget request, Widget desired,
  93.              ArgList args, Cardinal *num_args) {
  94.     return FALSE;
  95. }
  96.  
  97. static void Initialize(Widget request, Widget xnew, ArgList args, Cardinal *n);
  98. static void Resize(Widget gw);
  99.  
  100.  
  101. static void Realize(Widget w, XtValueMask *valuemask, XSetWindowAttributes *winattr) {
  102.     *valuemask |= CWBorderPixel | CWEventMask | CWBackingStore | CWBitGravity;
  103.     winattr->backing_store = WhenMapped;
  104.     winattr->border_pixel = blackpixel;
  105.     winattr->bit_gravity = NorthWestGravity;
  106.     winattr->event_mask = KeyPressMask | ExposureMask | ButtonPressMask |
  107.             ButtonReleaseMask | StructureNotifyMask | Button3MotionMask;
  108.    (*(tableauClassRec.core_class.superclass)->core_class.realize)(w, valuemask, winattr);
  109. }
  110.  
  111. TableauClassRec tableauClassRec = {
  112.   { /* core fields */
  113.     /* superclass        */    (WidgetClass) &widgetClassRec,
  114.     /* class_name        */    "Tableau",
  115.     /* widget_size        */    sizeof(TableauRec),
  116.     /* class_initialize        */    NULL,
  117.     /* class_part_initialize    */    NULL,
  118.     /* class_inited        */    FALSE,
  119.     /* initialize        */    Initialize,
  120.     /* initialize_hook        */    NULL,
  121.     /* realize            */    Realize,
  122.     /* actions            */    actions,
  123.     /* num_actions        */    XtNumber(actions),
  124.     /* resources        */    resources,
  125.     /* num_resources        */    XtNumber(resources),
  126.     /* xrm_class        */    NULLQUARK,
  127.     /* compress_motion        */    TRUE,
  128.     /* compress_exposure    */    TRUE,
  129.     /* compress_enterleave    */    TRUE,
  130.     /* visible_interest        */    FALSE,
  131.     /* destroy            */    NULL,
  132.     /* resize            */    Resize,
  133.     /* expose            */    Redisplay,
  134.     /* set_values        */    SetValues,
  135.     /* set_values_hook        */    NULL,
  136.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  137.     /* get_values_hook        */    NULL,
  138.     /* accept_focus        */    NULL,
  139.     /* version            */    XtVersion,
  140.     /* callback_private        */    NULL,
  141.     /* tm_table            */    translations,
  142.     /* query_geometry        */    XtInheritQueryGeometry,
  143.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  144.     /* extension        */    NULL
  145.   },
  146.   { /* tableau fields */
  147.     /* empty            */    0
  148.   }
  149. };
  150.  
  151. WidgetClass tableauWidgetClass = (WidgetClass)&tableauClassRec;
  152.  
  153.  
  154.  
  155. static Widget toplev = NULL;
  156.  
  157. static void Resize(Widget gw) {
  158.     TableauWidget w = (TableauWidget) gw;
  159.     /* printf("Parent wants me to have size %d,%d\n",
  160.        w->core.width, w->core.height); */
  161.     resize_event(w->core.width, w->core.height);
  162.     /* (*pileWidgetClass->core_class.superclass->core_class.resize)(gw); */
  163. }
  164.  
  165. void AskWidgetForResize(XSize_t x, XSize_t y) {
  166. #ifdef useMotif
  167.     Arg Args[2];
  168.     XtSetArg(Args[0], XtNwidth, x);
  169.     XtSetArg(Args[1], XtNheight, y);
  170.     if (toplev)
  171.     XtSetValues(toplev, Args, 2);
  172. #else
  173.     XtWidgetGeometry Geo;
  174.     XtGeometryResult r;
  175.  
  176.     Geo.width = x;
  177.     Geo.height = y;
  178.     do {
  179.     Geo.request_mode = CWWidth | CWHeight;
  180. #ifdef LABER
  181.     printf("resize to %d %d yielded ", Geo.width, Geo.height);
  182. #endif
  183.     r = XtMakeGeometryRequest(toplev, &Geo, &Geo);
  184. #ifdef LABER
  185.     switch (r) {
  186.     case XtGeometryYes:    printf("YES!\n");break;
  187.     case XtGeometryNo:    printf("NO!\n"); break;
  188.     case XtGeometryAlmost:printf("Almost!\n"); break;
  189.     case XtGeometryDone:    printf("Done!\n"); break;
  190.     }
  191. #endif
  192.     } while (r == XtGeometryAlmost);
  193. #endif
  194. }
  195.  
  196. static GC simpleGC(Pixel bg, Pixel fg) {
  197.     XGCValues gcv;
  198.     long gcflags;
  199.  
  200.     gcv.background = bg;
  201.     gcv.foreground = fg;
  202.     gcv.graphics_exposures = True;
  203.     gcflags = GCForeground | GCBackground | GCGraphicsExposures;
  204.     return XCreateGC(dpy, RootWindow(dpy, screen), gcflags, &gcv);
  205. }
  206.  
  207.  
  208. #define CHECK(x, mi, ma)    { if ((x) < mi || (x) > ma) (x) = (mi); }
  209. extern char *loadfilename;
  210. extern GC hintgc;
  211.  
  212. static void Initialize(Widget request, Widget xnew, ArgList args, Cardinal *n) {
  213.     static int is_inited = 0;
  214.     TableauWidget new = (TableauWidget)xnew;
  215.     TableauPart *p = &new->tableau;
  216.     toplev = xnew;
  217.     if (is_inited) {
  218.     fprintf(stderr, "Sorry, currently only one instance of Tableau is allowed\n");
  219.     exit(EXIT_FAILURE);
  220.     }
  221.  
  222.     {  const char *s; langdir = (s = getenv("LANG")) ? s : ""; }
  223.     read_message_file(p->messagefile ? p->messagefile : "messages");
  224.     read_keyboard_file(p->keyboardfile ? p->keyboardfile : "Keys");
  225.  
  226.     /* assign global data for old Xlib program */
  227.     dpy    = XtDisplay(new);
  228.     screen = DefaultScreen(dpy);
  229.     graphic.is_color = (DisplayCells(dpy, screen) > 2);
  230.     /* validity checks */
  231.     CHECK(p->decks,    -1, 9);
  232.     CHECK(p->slots,    -1, 32);
  233.     CHECK(p->faceup,     -1, 30);
  234.     CHECK(p->facedown,    -1, 30);
  235.     CHECK(p->jokers,    -1, 16);
  236.     CHECK(p->tmps,    -1, 16);
  237.     CHECK(p->param0,    -1, 99);
  238.     CHECK(p->param1,    -1, 99);
  239.     CHECK(p->param2,    -1, 99);
  240.     CHECK(p->param3,    -1, 99);
  241.  
  242.     rng = p->rng;
  243.     if (loadfilename)
  244.     load_game(loadfilename);
  245.     else {
  246.     long orgseed;
  247.     orgseed = game.seed;    /* this is destroyed by new_rules */
  248.     if (!p->rules)
  249.         p->rules = "Gypsy";
  250.     new_rules(p->rules, p->decks, p->slots, p->faceup,
  251.           p->facedown, p->jokers, p->tmps, p->param0, p->param1, p->param2, p->param3);
  252.     newgame(orgseed);
  253.     }
  254.     init_gfx();        /* make GCs */
  255.     init_cards(
  256. #ifdef LOADCARDS
  257.            p->cardset,
  258. #else
  259.            NULL,
  260. #endif
  261.            -1, -1, p->redcolor, p->cbcolor, 1, p->xpmdir);
  262.     if (p->arrowwidth <= 30 && p->arrowheight <= 30) {
  263.     graphic.aw = p->arrowwidth;
  264.     graphic.ah = p->arrowheight;
  265.     }
  266.     if (graphic.aw > 0 && graphic.ah > 0)
  267.     hintgc = simpleGC(BlackPixel(dpy, screen), p->arrowcolor);
  268.  
  269.     graphic.xgap = p->xgap;
  270.     graphic.ygap = p->ygap;
  271.     graphic.autolayout = 1;
  272. #ifdef LABER
  273.     printf("size = (%d,%d) and (%d,%d) in Initialize(), computing minwindow:\n",
  274.        graphic.width, graphic.height, new->core.width, new->core.height);
  275. #endif
  276.     generic_minwindow((XSize_t *)&graphic.width, (XSize_t *)&graphic.height);
  277.     graphic.min_width = graphic.width;
  278.     graphic.min_height = graphic.height;
  279. #ifdef LABER
  280.     printf("result is (%d,%d)\n", graphic.width, graphic.height);
  281. #endif
  282.  
  283.     init_mark(p->markcolor, p->markwidth);    /* initialize card marks */
  284.  
  285.     /* make icon here */
  286.     /* now the size is known */
  287. #ifdef LABER
  288.     printf("Init widget: res %d grap %d\n", new->core.width, graphic.width);
  289. #endif
  290.     if (new->core.width < graphic.width)
  291.     new->core.width = graphic.width;
  292.     if (new->core.height < graphic.height)
  293.     new->core.height = graphic.height;
  294. }
  295. #endif
  296.